﻿
#include "hoveringRippleButton.h"
#pragma execution_character_set("utf-8")
//悬浮波纹按钮
HoveringRippleButton::HoveringRippleButton(QWidget *parent)
    : QFrame(parent)
{
    geometry=QRect(0,0,100,50);
    minSize =QSize(100,50);
    initUI();
}

HoveringRippleButton::HoveringRippleButton(QWidget *parent, const QRect &geometry, const QSize &minSize)
    : QFrame(parent), geometry(geometry), minSize(minSize)
{
    initUI();
}

HoveringRippleButton::~HoveringRippleButton()
{

}

void HoveringRippleButton::initUI()
{
    setMinimumSize(minSize);
    setStyleSheet("QFrame{"
                  "	background-color: rgb(46, 22, 177);"
                  "	border:none;"
                  "	border-radius:10px;"
                  "}"
                  "QPushButton{"
                  "	background-color: rgba(255, 255, 255, 0);"
                  "	color: rgb(255, 255, 255);"
                  "}");

    m_pPushButton = new QPushButton(this);
    m_pPushButton->setMinimumSize(minSize);
    m_pPushButton->setFixedSize(QSize(width(), height()));
    QFont font;
    font.setPointSize(25);
    m_pPushButton->setFont(font);
    m_pPushButton->setText("悬浮会变色喔");

    setGeometry(geometry);
    initAnimationConfig();
}

void HoveringRippleButton::initAnimationConfig()
{
    corner_radius = 10;                                     // 按钮的圆角半径
    radius_var = 2;                                         // 半径变化值
    radius = 0;                                             // 起始半径
    max_radius = qSqrt(width() * width() + height() * height());  // 最大半径
    center = QPoint();                                       // 鼠标点击坐标
    color = QColor(255, 89, 0);                              // 填充颜色
    msec = 10;                                               // 定时时间

    m_pTimer = new QTimer(this);
    m_pTimer->setInterval(msec);
    connect(m_pTimer,SIGNAL(timeout()), this, SLOT(incRadius()));
}

//重写处理鼠标光标进入部件的事件
void HoveringRippleButton::enterEvent(QEvent *event)
{
    // 调用父类的 enterEvent 函数，以保持默认行为
    QFrame::enterEvent(event);
    /* QCursor::pos() 获取当前鼠标的全局坐标
     * mapFromGlobal()将全局坐标转换为窗口内部相对坐标*/
    center = mapFromGlobal(QCursor::pos());
    m_pTimer->disconnect();
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(incRadius()));
    m_pTimer->start();
}

//重写处理鼠标光标离开部件的事件
void HoveringRippleButton::leaveEvent(QEvent *event)
{
    // 调用父类的 leaveEvent 函数，以保持默认行为
    QFrame::leaveEvent(event);
    center = mapFromGlobal(QCursor::pos());
    m_pTimer->disconnect();
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(decRadius()));
    m_pTimer->start();
}

//重写绘制事件
void HoveringRippleButton::paintEvent(QPaintEvent *event)
{
     // 调用父类的 paintEvent 函数，以保持默认行为
    QFrame::paintEvent(event);

    if (center.isNull()) {
        return;
    }

    QPainter painter(this);
    //设置渲染提示; QPainter::Antialiasing 是一种渲染提示，用于抗锯齿绘制，使得图形边缘更加平滑
    painter.setRenderHint(QPainter::Antialiasing);

    QBrush brush(color);
    painter.setBrush(brush);
    //设置画笔; Qt::NoPen 表示不使用画笔，也就是不绘制边框
    painter.setPen(Qt::NoPen);

    QPainterPath path;
    //添加一个带有圆角的矩形路径
    path.addRoundedRect(rect(), corner_radius, corner_radius);
    //设置剪裁路径，即限制绘制区域为指定的路径范围内
    painter.setClipPath(path);
    //绘制椭圆（长轴==长轴，就圆形）
    painter.drawEllipse(center, radius, radius);
}

void HoveringRippleButton::incRadius()
{
    radius += radius_var;
    if (radius > max_radius) {
        m_pTimer->stop();
        return;
    }
    update();
}

void HoveringRippleButton::decRadius()
{
    radius -= radius_var;
    if (radius < 0) {
        m_pTimer->stop();
        return;
    }

    update();
}
